"""
Case Type   : 操作系统类-文件系统故障
Case Name   : switchover时，新主机注入故障系统文件句柄耗尽
Create At   : 2021/7/30
Owner       : li-xin12345
Description :
    1、开启极致RTO
    2、备节点注入文件句柄耗尽故障
    3、备节点上执行gs_ctl switchover
    4、清理故障，
    5、再次build
    6、执行TPCC
    7、检查数据一致性
    8、恢复环境
Expect      :
    1、开启极致RTO成功
    2、故障注入成功
    3、build失败
    4、清理故障成功
    5、build成功
    6、执行TPCC成功
    7、数据一致
    8、恢复环境
History     :
"""
import time
import unittest

from testcase.utils.ComThread import ComThread
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node, macro


class ReliabilityFileSystem(unittest.TestCase):
    nodes = ("PrimaryDbUser", "Standby1DbUser", "Standby2DbUser")

    @RestartDbCluster(*nodes)
    def setUp(self):
        self.log = Logger()
        self.primary_sh = CommonSH(self.nodes[0])
        self.sta1_sh = CommonSH(self.nodes[1])
        self.primary_root_node = Node(node="PrimaryRoot")
        self.sta1_root_node = Node(node="Standby1Root")
        self.sta1_node = Node(node="Standby1DbUser")
        self.constant = Constant()
        self.com = Common()
        self.log.info("setUp-实例化:ok")

        self.log.info("Opengauss_Reliability_OS_File_System_Case063:开始执行")
        self.inject_cmd = f"rfile_deplenish (timeout) values (120)"

    def test_main(self):
        text = "--step1:开启极致RTO;expect:成功"
        self.log.info(text)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "replication_type=1")
        self.log.info(result)
        self.assertTrue(result, "执行失败" + text)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "recovery_parse_workers=4")
        self.log.info(result)
        self.assertTrue(result, "执行失败" + text)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "recovery_redo_workers=4")
        self.log.info(result)
        self.assertTrue(result, "执行失败" + text)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "hot_standby=off")
        self.log.info(result)
        self.assertTrue(result, "执行失败" + text)
        result = self.primary_sh.stopDbCluster()
        self.assertTrue(result, "执行失败" + text)
        self.primary_sh.startDbCluster()
        db_status = self.primary_sh.getDbClusterStatus("detail")
        self.log.info(db_status)
        self.assertTrue("Normal" in db_status or "Degraded" in db_status)

        try:
            text = "--step2:备节点注入句柄耗尽故障;expect:成功"
            self.log.info(text)
            self.log.info("注入故障")
            session = ComThread(self.com.cfe_inject,
                                args=(self.sta1_root_node,
                                      self.inject_cmd))
            session.setDaemon(True)
            session.start()

            session.join(60)
            result = session.get_result()
            self.log.info(result)
            self.assertIn("successful execution", result, "执行失败" + text)

            text = "--step3:备节点上执行gs_ctl switchover;expect:失败"
            self.log.info(text)
            result = self.sta1_sh.executeGsctl(
                "switchover", self.constant.SWITCHOVER_SUCCESS_MSG,
                get_detail=True)
            self.log.info(result)
            self.assertIn(self.constant.TOO_MANYY_FILE_MSG, result,
                          "执行失败" + text)
            self.assertNotIn(self.constant.SWITCHOVER_SUCCESS_MSG, result,
                             "执行失败" + text)

        finally:
            text = "--step4:清除故障;expect:成功"
            self.log.info(text)
            result = self.com.cfe_query(self.sta1_root_node,
                                        "rfile_deplenish")
            self.log.info(result)
            if self.constant.TOO_MANYY_FILE_MSG in result:
                time.sleep(120)

        text = "--step5:故障清理后，备节点上再次执行gs_ctl switchover;expect:成功"
        self.log.info(text)
        result = self.sta1_sh.executeGsctl(
            "switchover", self.constant.SWITCHOVER_SUCCESS_MSG,
            get_detail=True)
        self.log.info(result)
        self.assertIn("server started", result, "执行失败" + text)
        self.assertNotIn(self.constant.TOO_MANYY_FILE_MSG, result,
                         "执行失败" + text)

        status = self.sta1_sh.exec_refresh_conf()
        self.assertTrue(status, "执行失败" + text)

        db_status = self.primary_sh.getDbClusterStatus("detail")
        self.log.info(db_status)
        self.assertTrue("Degraded" in db_status or "Normal" in db_status)
        self.assertTrue("S Primary" in db_status
                        and "P Standby" in db_status)

        text = "--step6:运行TPCC;expect:成功"
        self.log.info(text)
        result = self.com.startTPCC(self.sta1_root_node, macro.TPCC_PATH)
        self.log.info(result)
        self.assertIn(self.constant.TPCC_SUCCESS_MSG, result, "执行失败" + text)

        self.log.info("检查主备是否同步")
        time.sleep(10)
        self.log.info("获取节点数量")
        node_num = self.sta1_sh.get_node_num()
        res = self.sta1_sh.check_location_consistency("primary", node_num)
        self.assertTrue(res, "执行失败" + text)

        text = "--step7:检查数据一致性;expect:成功"
        self.log.info(text)
        nodes = (Node(self.nodes[0]), Node(self.nodes[1]),
                 Node(self.nodes[2]))
        flag = self.com.check_data_sample_by_all(r'\d', *nodes)
        self.assertTrue(flag, "执行失败" + text)
        table_dict = self.com.format_sql_result(
            self.sta1_sh.executDbSql(r'\d'))
        table_name = table_dict.get('Name')
        for name in table_name:
            select_sql = f"select count(*) from {name};"
            flag = self.com.check_data_sample_by_all(select_sql, *nodes)
            self.assertTrue(flag, "执行失败" + text)

    def tearDown(self):
        text = "--step8:环境清理;expect:成功"
        self.log.info(text)
        result = self.com.cfe_query(self.sta1_root_node,
                                    "rfile_deplenish")
        self.log.info(result)
        if self.constant.TOO_MANYY_FILE_MSG in result:
            time.sleep(120)
        self.log.info("故障已清理")

        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "replication_type=1")
        self.log.info(result)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "recovery_parse_workers=1")
        self.log.info(result)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "recovery_redo_workers=1")
        self.log.info(result)
        result = self.primary_sh.execute_gsguc("set",
                                               self.constant.GSGUC_SUCCESS_MSG,
                                               "hot_standby=on")
        self.log.info(result)
        result = self.primary_sh.stopDbCluster()
        self.assertTrue(result, "执行失败" + text)
        self.primary_sh.startDbCluster()

        db_status = self.primary_sh.getDbClusterStatus("detail")
        self.log.info(db_status)
        if "S Primary" in db_status or "P Standby" in db_status:
            result = self.primary_sh.executeGsctl("build",
                                                  "server started",
                                                  "-b full",
                                                  get_detail=True)
            self.log.info(result)
            result = self.primary_sh.executeGsctl(
                "switchover", self.constant.SWITCHOVER_SUCCESS_MSG,
                get_detail=True)
            self.log.info(result)
            status = CommonSH(self.nodes[0]).exec_refresh_conf()
            self.log.info(status)
        db_status = self.primary_sh.getDbClusterStatus("detail")
        self.log.info(db_status)
        result = Node(self.nodes[1]).sh("date").result()
        self.assertNotIn(self.constant.TOO_MANYY_FILE_MSG, result,
                         "执行失败" + text)
        self.log.info("Opengauss_Reliability_OS_File_System_Case063:执行完成")